package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.entity.ForumNotification;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumNotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 論壇通知控制器
 */
@RestController
@RequestMapping("/api/forum/notification")
public class ForumNotificationController {

    @Autowired
    private ForumNotificationService forumNotificationService;

    /**
     * 創建通知
     */
    @PostMapping
    public CommonResponse<Boolean> createNotification(@RequestParam Integer userId,
                                                    @RequestParam Integer senderId,
                                                    @RequestParam Integer type,
                                                    @RequestParam Integer targetType,
                                                    @RequestParam Integer targetId,
                                                    @RequestParam String content) {
        ForumNotification notif = new ForumNotification();
        notif.setUserId(userId);
        notif.setSenderId(senderId);
        notif.setType(type);
        notif.setTargetType(targetType);
        notif.setTargetId(targetId);
        notif.setContent(content);
        notif.setIsRead(0);
        boolean result = forumNotificationService.createNotification(notif);
        return CommonResponse.success(result);
    }

    /**
     * 標記通知為已讀
     */
    @PutMapping("/{notificationId}/read")
    public CommonResponse<Boolean> markAsRead(@PathVariable Integer notificationId, @RequestParam Integer userId) {
        boolean ok = forumNotificationService.markAsRead(notificationId, userId);
        return CommonResponse.success(ok);
    }

    /**
     * 標記所有通知為已讀
     */
    @PutMapping("/{userId}/read-all")
    public CommonResponse<Boolean> markAllAsRead(@PathVariable Integer userId) {
        try {
            boolean result = forumNotificationService.markAllAsRead(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("標記所有已讀失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢用戶通知列表（按時間降序）
     */
    @GetMapping("/user/{userId}")
    public CommonResponse<List<ForumNotification>> getNotifications(@PathVariable Integer userId) {
        List<ForumNotification> list = forumNotificationService.getNotificationsByUser(userId);
        return CommonResponse.success(list);
    }

    /**
     * 查詢未讀數
     */
    @GetMapping("/unreadCount/{userId}")
    public CommonResponse<Integer> getUnreadCount(@PathVariable Integer userId) {
        int count = forumNotificationService.countUnread(userId);
        return CommonResponse.success(count);
    }

    /**
     * 標記所有已讀
     */
    @PostMapping("/readAll/{userId}")
    public CommonResponse<Boolean> markAllRead(@PathVariable Integer userId) {
        boolean ok = forumNotificationService.markAllAsRead(userId);
        return CommonResponse.success(ok);
    }

    /**
     * 刪除通知
     */
    @DeleteMapping("/{notificationId}")
    public CommonResponse<Boolean> deleteNotification(@PathVariable Integer notificationId) {
        try {
            boolean result = forumNotificationService.deleteNotification(notificationId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("刪除通知失敗：" + e.getMessage());
        }
    }

    /**
     * 刪除所有通知
     */
    @DeleteMapping("/{userId}/all")
    public CommonResponse<Boolean> deleteAllNotifications(@PathVariable Integer userId) {
        try {
            boolean result = forumNotificationService.deleteAllNotifications(userId);
            return CommonResponse.success(result);
        } catch (Exception e) {
            return CommonResponse.error("刪除所有通知失敗：" + e.getMessage());
        }
    }
} 